package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.BatteryModelChart;
import com.tbit.main.service.AccountService;
import com.tbit.main.service.BatteryModelChartService;
import com.tbit.main.service.BatteryService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 电池型号
 *
 * @author LMY
 * @create 2021-05-27 16:08
 */
@RestController
@RequestMapping("/batteryModelChart")
public class BatteryModelChartController {

    @Autowired
    private BatteryModelChartService batteryModelChartService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private BatteryService batteryService;

    /**
     * 添加
     *
     * @param token
     * @param modelChart
     * @return
     */
    @RequestMapping("/insert")
    @OperateLog(module = ModuleEnum.BATTERYMODULE, methods = MethodEnum.BATTERY_MODULE_ADD)
    public JsonResult insert(String token, BatteryModelChart modelChart) {
        Assert.notNull(modelChart.getAccountId(), "AccountUser.agentId.notNull");
        Assert.notNull(modelChart.getBatteryBrand(), "BatteryModel.batterBrand.notNull");
        Assert.notNull(modelChart.getBatteryModel(), "BatteryModel.batterModel.notNull");
        Assert.notNull(modelChart.getBatteryCapacity(), "BatteryModel.batterCapacity.notNull");
        Assert.notNull(modelChart.getBatteryU(), "BatteryModel.batterU.notNull");
        Assert.notNull(modelChart.getBatteryI(), "BatteryModel.batterI.notNull");
        Assert.notNull(modelChart.getMaterials(), "BatteryModel.battermaterials.notNull");
        Assert.notNull(modelChart.getFullU(), "BatteryModel.batterfullU.notNull");
        Assert.notNull(modelChart.getLoseU(), "BatteryModel.batterloseU.notNull");
        Assert.notNull(modelChart.getMaxMileage(), "BatteryModel.battermaxMileage.notNull");

        AccountUser accountUser = tokenService.getAccount(token);

        modelChart.setAddTime(DateTimeUtil.getNowTime());
        modelChart.setAddAccountUserId(accountUser.getAccountUserId());

        modelChart.setAccountId(accountService.getByAccount(modelChart.getAccountId()));

        BatteryModelChart batteryModelChart = batteryModelChartService.getBatteryBrand(modelChart);
        if (batteryModelChart != null) {
            throw new BaseException("BatteryModel.batterModelBand.notNull");
        }

        batteryModelChartService.insert(modelChart);

        return JsonResult.succ();

    }

    /**
     * 修改
     *
     * @param token
     * @param modelChart
     * @return
     */
    @RequestMapping("/update")
    @OperateLog(module = ModuleEnum.BATTERYMODULE, methods = MethodEnum.BATTERY_MODULE_UPDATE)
    public JsonResult update(String token, BatteryModelChart modelChart) {

        Assert.notNull(modelChart.getAccountId(), "AccountUser.agentId.notNull");
        Assert.notNull(modelChart.getBatteryBrand(), "BatteryModel.batterBrand.notNull");
        Assert.notNull(modelChart.getBatteryModel(), "BatteryModel.batterModel.notNull");
        Assert.notNull(modelChart.getBatteryCapacity(), "BatteryModel.batterCapacity.notNull");
        Assert.notNull(modelChart.getBatteryU(), "BatteryModel.batterU.notNull");
        Assert.notNull(modelChart.getBatteryI(), "BatteryModel.batterI.notNull");
        Assert.notNull(modelChart.getBatteryModelId(), "BatteryModel.batterModelId.notNull");
        Assert.notNull(modelChart.getMaterials(), "BatteryModel.battermaterials.notNull");
        Assert.notNull(modelChart.getFullU(), "BatteryModel.batterfullU.notNull");
        Assert.notNull(modelChart.getLoseU(), "BatteryModel.batterloseU.notNull");
        Assert.notNull(modelChart.getMaxMileage(), "BatteryModel.battermaxMileage.notNull");

        AccountUser accountUser = tokenService.getAccount(token);

        modelChart.setAccountId(accountService.getByAccount(modelChart.getAccountId()));

        BatteryModelChart batteryModelChart = batteryModelChartService.getByModelId(modelChart.getBatteryModelId());

        /**替换可修改字段*/
        ObjectUtil.assign(modelChart, batteryModelChart, null);

        batteryModelChart.setUpdateAccountUserId(accountUser.getAccountUserId());

        BatteryModelChart batteryModelChartDB = batteryModelChartService.getBatteryBrand(modelChart);
        if (batteryModelChartDB != null && !batteryModelChart.getBatteryModelId().equals(batteryModelChartDB.getBatteryModelId())) {
            throw new BaseException("BatteryModel.batterModelBand.notNull");
        }

        batteryModelChartService.update(batteryModelChart);

        return JsonResult.succ();
    }

    /**
     * 删除
     *
     * @param token
     * @return
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.BATTERYMODULE, methods = MethodEnum.BATTERY_MODULE_DEL)
    public JsonResult del(String token, Integer batteryModelId) {
        Assert.notNull(batteryModelId, "BatteryModel.batterModelId.notNull");

        AccountUser accountUser = tokenService.getAccount(token);

        Integer batteryCount = batteryService.getByAccountIdCount(null, null, batteryModelId);
        if (batteryCount != 0) {
            throw new BaseException("batteryModel.battery.exit");
        }
        batteryModelChartService.del(batteryModelId);

        return JsonResult.succ();
    }

    /**
     * 根据运营区域查询
     *
     * @param token
     * @param accountId
     * @return
     */
    @RequestMapping("/getByAccountId")
    public JsonResult getByAccountId(String token, Integer accountId) {

        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        List<BatteryModelChart> modelCharts = batteryModelChartService.getByAccountId(accountService.getByAccount(accountId));

        return JsonResult.succ(modelCharts);
    }

}
